package com.ciei.dpagm.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.controller.BaseController;
import com.ciei.dpagm.entity.Region;
import com.ciei.dpagm.entity.Users;
import com.ciei.dpagm.service.RegionService;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.JsonUtil;
import com.ciei.dpagm.util.PageParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class RegionController extends BaseController {
	@Autowired
	private RegionService regionService;


	@RequestMapping("region/regionPage")
	public String regionPage(){
		return "region/regionPage";
	}

	@RequestMapping("region/regionForm")
	public String regionForm(){
		return "region/regionForm";
	}

	/**
	 * 加载区域数据，以网格形式显示
	 * @return
	 */
	@RequestMapping("region/loadGrid")
	@ResponseBody
	public JSONObject loadGrid() {
		PageParam ps=getPageParams();
		Map<String, Object> params=ps.params;
		int posStart =ps.posStart;
		int count =ps.count;
		int totalCount=0;
		
		List<Region> lists = null;
		JSONObject json = new JSONObject();
		JSONArray jarr = new JSONArray();
		params.put("isDelete",0);
		lists = regionService.findPage(posStart, count, params, new String[] {"regionName"}, new String[] {"isDelete"}, null);
		int index = 1;
		if(lists != null && lists.size() > 0) {
			for (Region region : lists) {

				Map<String, Object> mobj = new HashMap<String, Object>();
				mobj.put("rowNumber", index++);
				mobj.put("id", region.getRegionId());
				mobj.put("regionId", region.getRegionId());
				mobj.put("regionName", region.getRegionName());
				jarr.add(mobj);
			}	
		}
		json.put("rows", jarr);
		totalCount = regionService.getTotalCount( params,  new String[] {"regionName"}, new String[] {"isDelete"});
		json.put(APP.DATA, jarr);
		json.put(APP.TOTAL_COUNT, totalCount);
		json.put(APP.POS, posStart);
		return json;
	}

	/**
	 * 加载区域下拉框
	 * @return
	 */
	@RequestMapping("region/loadCombo")
	@ResponseBody
	public JSONObject loadCombo() {

		List<Region> lists = null;
		JSONObject json = new JSONObject();
		JSONArray jarr = new JSONArray();

		lists = regionService.findByProperty("isDelete",0);
		//lists.sort(Comparator.comparing(Region::getOrderId));

		if(lists != null && lists.size() > 0) {
			for (Region region : lists) {
				Map<String, Object> mobj = new HashMap<String, Object>();
				mobj.put("value", region.getRegionId());
				mobj.put("text", region.getRegionName());
				jarr.add(mobj);
			}
		}
		json.put("options", jarr);
		return json;
	}

	@RequestMapping("region/loadForm")
	@ResponseBody
	public JSONObject loadForm(Integer regionId) {

		Region region=regionService.findById(regionId);
		JSONObject json=new JSONObject();
		if(region!=null) {
			json= JsonUtil.getSuccess("");
			json.put("data", region);
		}else
		{
			json=JsonUtil.getFail("没有找到区域信息");
		}
		return json;
	}
	@RequestMapping("region/add")
	@ResponseBody
	public JSONObject add() {
		Region region=(Region) getInstanceByParameters(Region.class);
		region.setIsDelete(0);
		return regionService.add(region);
	}
	@RequestMapping("region/update")
	@ResponseBody
	public JSONObject update() {
		Region region=(Region) getInstanceByParameters(Region.class);
		if (regionService.updateById(region))
		{
			return JsonUtil.getSuccess("OK");
		}else {
			return JsonUtil.getFail("更新失败");
		}

	}

	@RequestMapping("region/delete")
	public @ResponseBody JSONObject delete(String deleteIds) {
		JSONObject json;
		if (deleteIds!=null&&deleteIds!="")
		{
			String[] strs = deleteIds.split(",");
			for (String deleteId:strs)
			{
				Region region=regionService.findById(Integer.valueOf(deleteId));

//               testProjectService.update(testProject);
//				regionService.removeById(region);
				region.setIsDelete(1);
				regionService.updateById(region);
			}
//			PageParam pageParam=getPageParams();
//			Users user = (Users) findSessionKey(APP.SESSION_KEY_CURRENT_USER);

			json = JsonUtil.getSuccess("删除成功!");
			json.put("deleteIds",deleteIds);
			return  json;

		}else
		{

			json = JsonUtil.getSuccess("删除失败!");
			return  json;
		}

	}
	/**
	 * 加载公司的试验区域数据
	 * @param
	 * @return
	 */
	@RequestMapping("vue/region/loadTree")
	@ResponseBody
	public JSONObject loadTree() {
		Users user = (Users) SecurityUtils.getSubject().getPrincipal();
		return regionService.loadTreeVue(user.getCompanyId());
	}

	/**
	 * 区域管理-新增
	 */
	@RequiresPermissions("system:region:add")
	@RequestMapping("vue/region/add")
	@ResponseBody
	public JSONObject addVue(@RequestBody JSONObject jsonObject) {
		return regionService.addOrUpdate(jsonObject,"add");
	}

	/**
	 * 区域管理-编辑
	 */
	@RequiresPermissions("system:region:edit")
	@RequestMapping("vue/region/update")
	@ResponseBody
	public JSONObject updateVue(@RequestBody JSONObject jsonObject) {
		return regionService.addOrUpdate(jsonObject,"edit");
	}

	/**
	 * 区域管理-删除
	 */
	@RequiresPermissions("system:region:del")
	@RequestMapping("vue/region/delete")
	@ResponseBody
	public JSONObject deleteVue(@RequestBody JSONObject jsonObject) {
		return regionService.deleteVue(jsonObject);
	}

	/**
	 * 加载要更新的数据
	 * @author pp
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping("vue/region/load")
	@ResponseBody
	public JSONObject loadRegionDataVue(@RequestBody JSONObject jsonObject) {
		return regionService.loadRegionDataVue(jsonObject);
	}


}
